package cc.vv.baselibrary.util;

import android.content.ClipboardManager;
import android.content.Context;
import android.text.TextUtils;

import java.util.regex.Pattern;

/**
 * 字符串系列工具
 * @author by nanPengFei on 2016/9/29 13:48.
 */
public class LKStrUtils {

    private static LKStrUtils mInstance = null;

    public static LKStrUtils getInstance() {
        if (null == mInstance) {
            synchronized (LKStrUtils.class) {
                if (mInstance == null) {
                    mInstance = new LKStrUtils();
                }
            }
        }
        return mInstance;
    }

    /***
     *  正则表达式，判断首字母是否是英文字母
     */
    private static final Pattern ALPHA_PATTERN = Pattern.compile("^[A-Za-z]+$");

    /**
     * 获取汉语拼音首字母
     *
     * @param str
     * @return
     */
    public String getAlpha(String str) {
        if (str == null) {
            return "#";
        }
        if (str.trim().length() == 0) {
            return "#";
        }
        char c = str.trim().substring(0, 1).charAt(0);
        if (ALPHA_PATTERN.matcher(c + "").matches()) {
            return (c + "").toUpperCase();
        } else {
            return "#";
        }
    }


    /**
     * 实现文本复制功能
     * add by wangqianzhou
     *
     * @param content
     */
    public void copy(String content, Context context) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
    }

    /**
     * 实现粘贴功能
     * add by wangqianzhou
     *
     * @param context
     * @return
     */
    public String paste(Context context) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        return cmb.getText().toString().trim();
    }

    /**
     * 截取小数点后两位
     *
     * @param money
     * @return
     */
    public static String cutOutDoubleMoney(double money) {
        String mMoney = money + "";
        String newMoney = mMoney;
        if (!TextUtils.isEmpty(mMoney)) {
            if (mMoney.contains(".")) {
                int index = mMoney.indexOf('.');
                String substring = mMoney.substring(index, mMoney.length());
                if (substring.length() > 3) {
                    newMoney = mMoney.substring(0, index) + substring.substring(0, 3);
                } else if (substring.length() == 2) {
                    newMoney = mMoney.substring(0, index) + substring + "0";
                } else if (substring.length() == 1) {
                    newMoney = mMoney.substring(0, index) + substring + "00";
                }
            } else {
                newMoney = mMoney + ".00";
            }
        }
        return newMoney;
    }

    /**
     * 截取小数点后两位
     *
     * @param money
     * @return
     */
    public static String cutOutDoubleMoney(String money) {
        String newMoney = money;
        if (!TextUtils.isEmpty(money)) {
            if (money.contains(".")) {
                int index = money.indexOf('.');
                String substring = money.substring(index, money.length());
                if (substring.length() > 3) {
                    newMoney = money.substring(0, index) + substring.substring(0, 3);
                } else if (substring.length() == 2) {
                    newMoney = money.substring(0, index) + substring + "0";
                } else if (substring.length() == 1) {
                    newMoney = money.substring(0, index) + substring + "00";
                }
            } else {
                newMoney = money + ".00";
            }
        }
        return newMoney;
    }
}
